package dp.mobile.system;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import android.util.Log;

public class XMLReader {
	private static final String TAG = "XML Reader";
	
	public Node parseXML(String filePath) {
		try {
			//Log.d(TAG, "reading " + filePath);
			return parseXML(new FileInputStream(new File(filePath)));
		} catch (FileNotFoundException fEX) {
			Log.e(TAG, "" + fEX);
		}
		
		return null;
	}
	
	public Node parseXML(InputStream stream) {
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			dbf.setIgnoringElementContentWhitespace(true);
			DocumentBuilder db 	= dbf.newDocumentBuilder();
			Document dom 		= db.parse(stream);
			Node root 			= dom.getDocumentElement();
			
			//Log.d(TAG, "root = " + root.getNodeName());
			
			return root;
		} catch (ParserConfigurationException pEX) {
			Log.i(TAG, "XML Error disini " + pEX);
		} catch (IOException iEx) {
			
		} catch (SAXException sEX) {
			
		}
		
		return null;
	}
	
	public static XMLReader instance() {
		if (sInstance == null)
			sInstance = new XMLReader();
		
		return sInstance;
	}
	
	private static XMLReader sInstance;
}
